* Change the directory to the top one for this project
set more off
capture cd "C:\Users\Benjamin Allen\Loewen-RubensonLab Dropbox\LR Research Lab - Shared\Papers\Support for Algorithmic Governance"
capture cd "C:\Users\balle\Loewen-RubensonLab Dropbox\LR Research Lab - Shared\Papers\Support for Algorithmic Governance"
capture cd "~\Loewen-RubensonLab Dropbox\LR Research Lab - Shared\Papers\Support for Algorithmic Governance"


********************************************************************************
* Converting and recoding country-level data
********************************************************************************

*------------------------------------------------------------------------------*
* Import excel data
*------------------------------------------------------------------------------*

import excel "data\Support for Algorithmic Governance country level data.xlsx", sheet("Sheet1") firstrow case(lower) clear

* This badly needs labelling. Based on the cobebook
/*
1-Austria
2-Belgium (FR)
3-Belgium (NL)
4-Denmark
5-Finland
6-France
7-Germany
8-Greece
9-Italy
10-Ireland
11-Netherlands
12-Norway
13-Portugal
14-Spain
15-Sweden
16-UK

I have added Canada to this, with a country code of 0.

*/

*------------------------------------------------------------------------------*
* Label variables
*------------------------------------------------------------------------------*

* I just changed the Excel sheet to use the recoded country variable (EPIS data recoded to use it below)

lab def country_recoded ///
	1 "Austria" ///
	2 "Belgium (FR)" ///
	3 "Belgium (NL)" ///
	4 "Canada" ///
	5 "Denmark" ///
	6 "Finland" ///
	7 "France" ///
	8 "Germany" ///
	9 "Greece" ///
	10 "Ireland" ///
	11 "Italy" ///
	12 "Netherlands" ///
	13 "Norway" ///
	14 "Portugal" ///
	15 "Spain" ///
	16 "Sweden" ///
	17 "UK"
lab val country_recoded country_recoded

lab li country_recoded
tab country_recoded

drop country_name

*------------------------------------------------------------------------------*
* Rescale variables
*------------------------------------------------------------------------------*

* Trust in others (country level, from World Values Survey)
sum trust_in_others_wvs
gen trust_in_others_wvs_rescaled = (trust_in_others_wvs -r(min)) / (r(max)-r(min))
sum trust_in_others_wvs_rescaled
lab var trust_in_others_wvs_rescaled "Country level trust in others (rescaled 0-1)"

* UN E-Government index
sum un_e_government_index
gen un_e_government_index_rescaled = (un_e_government_index -r(min)) / (r(max)-r(min))
sum un_e_government_index_rescaled
lab var un_e_government_index_rescaled "UN E-Government index (rescaled 0-1)"

* Digital skills index
sum digital_skills_index
gen digital_skills_index_rescaled = (digital_skills_index -r(min)) / (r(max)-r(min))
sum digital_skills_index_rescaled
lab var digital_skills_index_rescaled "Digital skills index (rescaled 0-1)"

* Leader approval
sum leader_approval
gen leader_approval_rescaled = (leader_approval -r(min)) / (r(max)-r(min))
sum leader_approval_rescaled
lab var leader_approval_rescaled "Leader approval (rescaled 0-1 worst-best)"

*------------------------------------------------------------------------------*
* Save as .dta
*------------------------------------------------------------------------------*

save "data/Support for Algorithmic Governance country level data rescaled.dta", replace

********************************************************************************
* EPIS data
********************************************************************************

* Load the EPIS data
use "data/EPIS_data.dta", clear

/* List of variables that I need, based on the Canadian analysis

A lot of these are scales, so I'll need the component variables for those

	sum age_in_years_rescaled ///
		female ///
		education_rescaled ///
		income_rescaled ///
		
		alg_govt_acceptable_scale_bi ///
		alg_govt_acc_eff_scale_bi ///
		alg_govt_acc_fair_scale_bi ///
		
		
		lr_scale_rescaled ///
		populism_scale

	sum alg_govt_acceptable_scale_ord ///
		alg_govt_acc_eff_scale_ord ///
		alg_govt_acc_fair_scale_ord

* Other things wee'll need:

	country
	
*/

********************************************************************************
* Recoding and labeling relevant variables
********************************************************************************

* I'll check each variable, even if I don't need to recode or label it

*------------------------------------------------------------------------------*
* startdate and enddate
*------------------------------------------------------------------------------*

rename startdate startdate_string
rename enddate enddate_string

gen startdate = clock(startdate_string, "YMD")
gen enddate = clock(enddate_string, "YMD")

format startdate %tc
format enddate %tc

* Note that as there aren't hours, minutes, days or seconds in this, so there's some false accuracy
note startdate: The original variable was just YYYY-MM-DD format, so the hours, minutes, and seconds are false accuracy
note enddate: The original variable was just YYYY-MM-DD format, so the hours, minutes, and seconds are false accuracy

*------------------------------------------------------------------------------*
* Country
*------------------------------------------------------------------------------*

* This badly needs labelling. Based on the cobebook
/*
1-Austria
2-Belgium (FR)
3-Belgium (NL)
4-Denmark
5-Finland
6-France
7-Germany
8-Greece
9-Italy
10-Ireland
11-Netherlands
12-Norway
13-Portugal
14-Spain
15-Sweden
16-UK
*/

* The order is alphabetical EXCEPT for Italy and Ireland. Checking against the language variable to make sure that that isn't a typo.
tab country user_language, m

/*
           |                                                            user_language
   Country |        DA         DE         EL         EN      ES-ES         FI         FR         IT         NL         NO         PT         SV |     Total
-----------+------------------------------------------------------------------------------------------------------------------------------------+----------
         1 |         0        978          0          0          0          0          0          0          0          0          0          0 |       978 
         2 |         0          0          0         41          0          0        939          0          0          0          0          0 |       980 
         3 |         0          0          0          0          0          0          0          0        865          0          0          0 |       865 
         4 |       997          0          0          0          0          0          0          0          0          0          0          0 |       997 
         5 |         0          0          0          0          0        977          0          0          0          0          0          0 |       977 
         6 |         0          0          0          0          0          0        977          0          0          0          0          0 |       977 
         7 |         0        935          0          0          0          0          0          0          0          0          0          0 |       935 
         8 |         0          0        788          0          0          0          0          0          0          0          0          0 |       788 
         9 |         0          0          0          0          0          0          0        990          0          0          0          0 |       990 
        10 |         0          0          0        989          0          0          0          0          0          0          0          0 |       989 
        11 |         0          0          0          0          0          0          0          0        975          0          0          0 |       975 
        12 |         0          0          0          0          0          0          0          0          0        992          0          0 |       992 
        13 |         0          0          0          0          0          0          0          0          0          0      1,003          0 |     1,003 
        14 |         0          0          0          0        992          0          0          0          0          0          0          0 |       992 
        15 |         0          0          0          0          0          0          0          0          0          0          0      1,001 |     1,001 
        16 |         0          0          0        975          0          0          0          0          0          0          0          0 |       975 
-----------+------------------------------------------------------------------------------------------------------------------------------------+----------
     Total |       997      1,913        788      2,005        992        977      1,916        990      1,840        992      1,003      1,001 |    15,414 

9 = "IT", so Italy is correct
10 = "EN", which makes sense for Ireland

*/

* However, this is confusing and not intuitive, and I'll need to add Canada in (and might as well make it alphabetical), so I'll switch to using a recoded country var

recode country ///
		(1  = 1 "Austria") ///
		(2  = 2 "Belgium (FR)") ///
		(3  = 3 "Belgium (NL)") ///
		(0  = 4 "Canada") /// Canada will get added to the data later
		(4  = 5 "Denmark") ///
		(5  = 6 "Finland") ///
		(6  = 7 "France") ///
		(7  = 8 "Germany") ///
		(8  = 9 "Greece") ///
		(10 = 10 "Ireland") ///
		(9  = 11 "Italy") ///
		(11 = 12 "Netherlands") ///
		(12 = 13 "Norway") ///
		(13 = 14 "Portugal") ///
		(14 = 15 "Spain") ///
		(15 = 16 "Sweden") ///
		(16 = 17 "UK") ///
	, gen(country_recoded) ///
	lab(country_recoded)

lab li country_recoded
tab country_recoded

* Checking language again
tab country_recoded user_language, m
* Looks good

*------------------------------------------------------------------------------*
* Age
*------------------------------------------------------------------------------*

sum age

hist age
* Looks reasonable

gen age_in_years = age
lab var age_in_years "Age in years"

*------------------------------------------------------------------------------*
* Gender
*------------------------------------------------------------------------------*

tab gender

gen female = 0
replace female = 1 if gender == "Female"

lab def female 0 "Not female" 1 "Female"
lab val female female

lab var female "Female (0/1)"

*------------------------------------------------------------------------------*
* Income
*------------------------------------------------------------------------------*

/* EPIS codebook:

Q11. What comes closest to how you feel about your household’s income nowadays?
1-Living comfortably on present income
2-Coping on present income
3-Finding it difficult on present income
4-Finding it very difficult on present income
5-Don’t know
6-Refusal

This needs to be re-ordered. Target ordering:

.	Refusal
.	Don’t know
0	Finding it very difficult on present income
1	Finding it difficult on present income
2	Coping on present income
3	Living comfortably on present income

*/

recode q11 ///
	(5 6 = . ) ///
	(4 = 0 "Finding it very difficult on present income") ///
	(3 = 1 "Finding it difficult on present income") ///
	(2 = 2 "Coping on present income") ///
	(1 = 3 "Living comfortably on present income") ///
	, gen(subjective_income) ///
	lab(subjective_income)

codebook subjective_income
tab subjective_income

*------------------------------------------------------------------------------*
* Education
*------------------------------------------------------------------------------*

tab education
* It looks like the categories are slightly different for each
bys country_recoded: tab education

sort country_recoded education

gen education_cat = .

*-----------------------------------------------------*
* Austria
*-----------------------------------------------------*

replace education_cat = 0 if country_recoded == "Austria":country_recoded & education == "Incomplete Secondary Education"

replace education_cat = 1 if country_recoded == "Austria":country_recoded & education == "Secondary Education Completed  (BaccalaurÃ©at or equivalent)"
replace education_cat = 1 if country_recoded == "Austria":country_recoded & education == "Some University or Vocational Certification"

replace education_cat = 2 if country_recoded == "Austria":country_recoded & education == "Vocational or Professional Certification Completed (BTS, DUT or equivalent)"

replace education_cat = 3 if country_recoded == "Austria":country_recoded & education == "University Education Completed (Bac+3)"
replace education_cat = 3 if country_recoded == "Austria":country_recoded & education == "Postgraduate Education Completed (Bac+5: Master, Engineering Degree or equivalent)"
replace education_cat = 3 if country_recoded == "Austria":country_recoded & education == "Doctorate, Post-doctorate or equivalent  (Bac +8)"

* Check the results
tab education if country_recoded == "Austria":country_recoded & education_cat == .

*-----------------------------------------------------*
* Belgium (FR)
*-----------------------------------------------------*

replace education_cat = 0 if country_recoded == "Belgium (FR)":country_recoded & education == "Incomplete Secondary Education"

replace education_cat = 1 if country_recoded == "Belgium (FR)":country_recoded & education == "Secondary Education Completed  (BaccalaurÃ©at or equivalent)"
replace education_cat = 1 if country_recoded == "Belgium (FR)":country_recoded & education == "Some University or Vocational Certification"

replace education_cat = 2 if country_recoded == "Belgium (FR)":country_recoded & education == "Vocational or Professional Certification Completed (BTS, DUT or equivalent)"

replace education_cat = 3 if country_recoded == "Belgium (FR)":country_recoded & education == "University Education Completed (Bac+3)"
replace education_cat = 3 if country_recoded == "Belgium (FR)":country_recoded & education == "Postgraduate Education Completed (Bac+5: Master, Engineering Degree or equivalent)"
replace education_cat = 3 if country_recoded == "Belgium (FR)":country_recoded & education == "Doctorate, Post-doctorate or equivalent  (Bac +8)"

* Check the results
tab education if country_recoded == "Belgium (FR)":country_recoded & education_cat == .

*-----------------------------------------------------*
* Belgium (NL)
*-----------------------------------------------------*

replace education_cat = 0 if country_recoded == "Belgium (NL)":country_recoded & education == "Incomplete Secondary Education"

replace education_cat = 1 if country_recoded == "Belgium (NL)":country_recoded & education == "Secondary Education Completed  (BaccalaurÃ©at or equivalent)"
replace education_cat = 1 if country_recoded == "Belgium (NL)":country_recoded & education == "Some University or Vocational Certification"

replace education_cat = 2 if country_recoded == "Belgium (NL)":country_recoded & education == "Vocational or Professional Certification Completed (BTS, DUT or equivalent)"

replace education_cat = 3 if country_recoded == "Belgium (NL)":country_recoded & education == "University Education Completed (Bac+3)"
replace education_cat = 3 if country_recoded == "Belgium (NL)":country_recoded & education == "Postgraduate Education Completed (Bac+5: Master, Engineering Degree or equivalent)"
replace education_cat = 3 if country_recoded == "Belgium (NL)":country_recoded & education == "Doctorate, Post-doctorate or equivalent  (Bac +8)"

* Check the results
tab education if country_recoded == "Belgium (NL)":country_recoded & education_cat == .

*-----------------------------------------------------*
* Denmark
*-----------------------------------------------------*

replace education_cat = 0 if country_recoded == "Denmark":country_recoded & education == "Incomplete Secondary Education"

replace education_cat = 1 if country_recoded == "Denmark":country_recoded & education == "Secondary Education Completed  (BaccalaurÃ©at or equivalent)"
replace education_cat = 1 if country_recoded == "Denmark":country_recoded & education == "Some University or Vocational Certification"

replace education_cat = 2 if country_recoded == "Denmark":country_recoded & education == "Vocational or Professional Certification Completed (BTS, DUT or equivalent)"

replace education_cat = 3 if country_recoded == "Denmark":country_recoded & education == "University Education Completed (Bac+3)"
replace education_cat = 3 if country_recoded == "Denmark":country_recoded & education == "Postgraduate Education Completed (Bac+5: Master, Engineering Degree or equivalent)"
replace education_cat = 3 if country_recoded == "Denmark":country_recoded & education == "Doctorate, Post-doctorate or equivalent  (Bac +8)"

* Check the results
tab education if country_recoded == "Denmark":country_recoded & education_cat == .

*-----------------------------------------------------*
* Finland
*-----------------------------------------------------*

replace education_cat = 0 if country_recoded == "Finland":country_recoded & education == "Incomplete Secondary Education"

replace education_cat = 1 if country_recoded == "Finland":country_recoded & education == "Secondary Education Completed  (BaccalaurÃ©at or equivalent)"
replace education_cat = 1 if country_recoded == "Finland":country_recoded & education == "Some University or Vocational Certification"

replace education_cat = 2 if country_recoded == "Finland":country_recoded & education == "Vocational or Professional Certification Completed (BTS, DUT or equivalent)"

replace education_cat = 3 if country_recoded == "Finland":country_recoded & education == "University Education Completed (Bac+3)"
replace education_cat = 3 if country_recoded == "Finland":country_recoded & education == "Postgraduate Education Completed (Bac+5: Master, Engineering Degree or equivalent)"
replace education_cat = 3 if country_recoded == "Finland":country_recoded & education == "Doctorate, Post-doctorate or equivalent  (Bac +8)"

* Check the results
tab education if country_recoded == "Finland":country_recoded & education_cat == .

*-----------------------------------------------------*
* France
*-----------------------------------------------------*

replace education_cat = 0 if country_recoded == "France":country_recoded & education == "Incomplete Secondary Education"

replace education_cat = 1 if country_recoded == "France":country_recoded & education == "Secondary Education Completed  (BaccalaurÃ©at or equivalent)"
replace education_cat = 1 if country_recoded == "France":country_recoded & education == "Some University or Vocational Certification"

replace education_cat = 2 if country_recoded == "France":country_recoded & education == "Vocational or Professional Certification Completed (BTS, DUT or equivalent)"

replace education_cat = 3 if country_recoded == "France":country_recoded & education == "University Education Completed (Bac+3)"
replace education_cat = 3 if country_recoded == "France":country_recoded & education == "Postgraduate Education Completed (Bac+5: Master, Engineering Degree or equivalent)"
replace education_cat = 3 if country_recoded == "France":country_recoded & education == "Doctorate, Post-doctorate or equivalent  (Bac +8)"

* Check the results
tab education if country_recoded == "France":country_recoded & education_cat == .

*-----------------------------------------------------*
* Germany
*-----------------------------------------------------*

replace education_cat = 0 if country_recoded == "Germany":country_recoded & education == "Education not yet completed"

replace education_cat = 1 if country_recoded == "Germany":country_recoded & education == "Secondary Education Completed - Level 1"
replace education_cat = 1 if country_recoded == "Germany":country_recoded & education == "Secondary Education Completed - Level 2"
replace education_cat = 1 if country_recoded == "Germany":country_recoded & education == "Higher Secondary Education Diploma/Vocational Diploma"

replace education_cat = 2 if country_recoded == "Germany":country_recoded & education == "Vocational or Professional Certification Completed"

replace education_cat = 3 if country_recoded == "Germany":country_recoded & education == "University Education Completed"

* Check the results
tab education if country_recoded == "Germany":country_recoded & education_cat == .

*-----------------------------------------------------*
* Greece
*-----------------------------------------------------*

replace education_cat = 0 if country_recoded == "Greece":country_recoded & education == "Incomplete Secondary Education"

replace education_cat = 1 if country_recoded == "Greece":country_recoded & education == "Secondary Education Completed"
replace education_cat = 1 if country_recoded == "Greece":country_recoded & education == "Some University or Vocational Certification"

replace education_cat = 2 if country_recoded == "Greece":country_recoded & education == "Vocational or Professional Certification Completed"

replace education_cat = 3 if country_recoded == "Greece":country_recoded & education == "University Education Completed"
replace education_cat = 3 if country_recoded == "Greece":country_recoded & education == "Postgraduate Education Completed"
replace education_cat = 3 if country_recoded == "Greece":country_recoded & education == "Doctorate, Post-doctorate or equivalent Completed"

* Check the results
tab education if country_recoded == "Greece":country_recoded & education_cat == .

*-----------------------------------------------------*
* Italy
*-----------------------------------------------------*

replace education_cat = 0 if country_recoded == "Italy":country_recoded & education == "Incomplete Secondary Education"

replace education_cat = 1 if country_recoded == "Italy":country_recoded & education == "Secondary Education Completed"
replace education_cat = 1 if country_recoded == "Italy":country_recoded & education == "Some University or Vocational Certification"

replace education_cat = 2 if country_recoded == "Italy":country_recoded & education == "Vocational or Professional Certification Completed"

replace education_cat = 3 if country_recoded == "Italy":country_recoded & education == "University Education Completed"
replace education_cat = 3 if country_recoded == "Italy":country_recoded & education == "Postgraduate Education Completed"
replace education_cat = 3 if country_recoded == "Italy":country_recoded & education == "Doctorate, Post-doctorate or equivalent Completed"

* Check the results
tab education if country_recoded == "Italy":country_recoded & education_cat == .

*-----------------------------------------------------*
* Ireland
*-----------------------------------------------------*

replace education_cat = 0 if country_recoded == "Ireland":country_recoded & education == "Incomplete Secondary Education"

replace education_cat = 1 if country_recoded == "Ireland":country_recoded & education == "Secondary Education Completed"
replace education_cat = 1 if country_recoded == "Ireland":country_recoded & education == "Some University or Vocational Certification"

replace education_cat = 2 if country_recoded == "Ireland":country_recoded & education == "Vocational or Professional Certification Completed"

replace education_cat = 3 if country_recoded == "Ireland":country_recoded & education == "University Education Completed"
replace education_cat = 3 if country_recoded == "Ireland":country_recoded & education == "Postgraduate Education Completed"
replace education_cat = 3 if country_recoded == "Ireland":country_recoded & education == "Doctorate, Post-doctorate or equivalent Completed"

* Check the results
tab education if country_recoded == "Ireland":country_recoded & education_cat == .

*-----------------------------------------------------*
* Netherlands
*-----------------------------------------------------*

replace education_cat = 0 if country_recoded == "Netherlands":country_recoded & education == "Incomplete Secondary Education"

replace education_cat = 1 if country_recoded == "Netherlands":country_recoded & education == "Secondary Education Completed"
replace education_cat = 1 if country_recoded == "Netherlands":country_recoded & education == "Some University or Vocational Certification"

replace education_cat = 2 if country_recoded == "Netherlands":country_recoded & education == "Vocational or Professional Certification Completed"

replace education_cat = 3 if country_recoded == "Netherlands":country_recoded & education == "University Education Completed"
replace education_cat = 3 if country_recoded == "Netherlands":country_recoded & education == "Postgraduate Education Completed"
replace education_cat = 3 if country_recoded == "Netherlands":country_recoded & education == "Doctorate, Post-doctorate or equivalent Completed"

* Check the results
tab education if country_recoded == "Netherlands":country_recoded & education_cat == .

*-----------------------------------------------------*
* Norway
*-----------------------------------------------------*

replace education_cat = 0 if country_recoded == "Norway":country_recoded & education == "Incomplete Secondary Education"

replace education_cat = 1 if country_recoded == "Norway":country_recoded & education == "Secondary Education Completed  (BaccalaurÃ©at or equivalent)"
replace education_cat = 1 if country_recoded == "Norway":country_recoded & education == "Some University or Vocational Certification"

replace education_cat = 2 if country_recoded == "Norway":country_recoded & education == "Vocational or Professional Certification Completed (BTS, DUT or equivalent)"

replace education_cat = 3 if country_recoded == "Norway":country_recoded & education == "University Education Completed (Bac+3)"
replace education_cat = 3 if country_recoded == "Norway":country_recoded & education == "Postgraduate Education Completed (Bac+5: Master, Engineering Degree or equivalent)"
replace education_cat = 3 if country_recoded == "Norway":country_recoded & education == "Doctorate, Post-doctorate or equivalent  (Bac +8)"

* Check the results
tab education if country_recoded == "Norway":country_recoded & education_cat == .

*-----------------------------------------------------*
* Portugal
*-----------------------------------------------------*

replace education_cat = 0 if country_recoded == "Portugal":country_recoded & education == "Incomplete Secondary Education"

replace education_cat = 1 if country_recoded == "Portugal":country_recoded & education == "Secondary Education Completed  (BaccalaurÃ©at or equivalent)"
replace education_cat = 1 if country_recoded == "Portugal":country_recoded & education == "Some University or Vocational Certification"

replace education_cat = 2 if country_recoded == "Portugal":country_recoded & education == "Vocational or Professional Certification Completed (BTS, DUT or equivalent)"

replace education_cat = 3 if country_recoded == "Portugal":country_recoded & education == "University Education Completed (Bac+3)"
replace education_cat = 3 if country_recoded == "Portugal":country_recoded & education == "Postgraduate Education Completed (Bac+5: Master, Engineering Degree or equivalent)"
replace education_cat = 3 if country_recoded == "Portugal":country_recoded & education == "Doctorate, Post-doctorate or equivalent  (Bac +8)"

* Check the results
tab education if country_recoded == "Portugal":country_recoded & education_cat == .

*-----------------------------------------------------*
* Spain
*-----------------------------------------------------*

replace education_cat = 0 if country_recoded == "Spain":country_recoded & education == "Incomplete Secondary Education"

replace education_cat = 1 if country_recoded == "Spain":country_recoded & education == "Secondary Education Completed"
replace education_cat = 1 if country_recoded == "Spain":country_recoded & education == "Some University or Vocational Certification"

replace education_cat = 2 if country_recoded == "Spain":country_recoded & education == "Vocational or Professional Certification Completed"

replace education_cat = 3 if country_recoded == "Spain":country_recoded & education == "University Education Completed"
replace education_cat = 3 if country_recoded == "Spain":country_recoded & education == "Postgraduate Education Completed"
replace education_cat = 3 if country_recoded == "Spain":country_recoded & education == "Doctorate, Post-doctorate or equivalent Completed"

* Check the results
tab education if country_recoded == "Spain":country_recoded & education_cat == .

*-----------------------------------------------------*
* Sweden
*-----------------------------------------------------*

replace education_cat = 0 if country_recoded == "Sweden":country_recoded & education == "Primary education (Year 1 - 9) completed"

replace education_cat = 1 if country_recoded == "Sweden":country_recoded & education == "Secondary education completed"

replace education_cat = 2 if country_recoded == "Sweden":country_recoded & education == "KY Vocational education or other vocational education"

replace education_cat = 3 if country_recoded == "Sweden":country_recoded & education == "University - Academic education ground level (BA)"
replace education_cat = 3 if country_recoded == "Sweden":country_recoded & education == "University - Academic education advanced level (MA)"
replace education_cat = 3 if country_recoded == "Sweden":country_recoded & education == "University - Academic education doctoral level (PhD)"

* Check the results
tab education if country_recoded == "Sweden":country_recoded & education_cat == .

*-----------------------------------------------------*
* UK
*-----------------------------------------------------*

replace education_cat = 0 if country_recoded == "UK":country_recoded & education == "Incomplete Secondary Education  (Below GC SE / O Level)"

replace education_cat = 1 if country_recoded == "UK":country_recoded & education == "Secondary Education Completed"
replace education_cat = 1 if country_recoded == "UK":country_recoded & education == "Secondary Education Completed (A Level or equivalent)"
replace education_cat = 1 if country_recoded == "UK":country_recoded & education == "Secondary Education Completed (GCSE / O Level / CSE or equivalent)"
replace education_cat = 1 if country_recoded == "UK":country_recoded & education == "Some Vocational or Technical Qualifications"

replace education_cat = 2 if country_recoded == "UK":country_recoded & education == "Vocational or Technical Qualifications Completed (e.g. HND, NVQ)"

replace education_cat = 3 if country_recoded == "UK":country_recoded & education == "University Education Completed (First Degree e.g. BA, BSc)"
replace education_cat = 3 if country_recoded == "UK":country_recoded & education == "Postgraduate Education Completed  (e.g. Masters)"
replace education_cat = 3 if country_recoded == "UK":country_recoded & education == "Doctorate, Post-doctorate or equivalent  (Higher Degree)"

* Check the results
tab education if country_recoded == "UK":country_recoded & education_cat == .

*-----------------------------------------------------*
* Check to see if I missed any categories
*-----------------------------------------------------*

* DK resposnes were left as missing intentionally
bys country_recoded: tab education if education_cat == .

lab def education_cat ///
	///.	Prefer not to answer" ///
	0	"Incomplete Secondary Education" ///
	1	"Secondary Education Completed" ///
	2	"Vocational or Professional Certification Completed" ///
	3	"University Education Completed or higher"
lab val education_cat education_cat

lab var education_cat "Education (0-3)"

tab education_cat

* Double check that the country categories within these look reasonable
bys education_cat: tab education, m


*------------------------------------------------------------------------------*
* Acceptability of algorithmic governance
*------------------------------------------------------------------------------*

/*

* CANADIAN

Acceptable reason to use an algorithm (1)
Unacceptable reason to use an algorithm (2)
Not sure (3)

To reduce the time required to make decisions. (1) 
To make decisions which will be a better use of government money. (2) 
To make sure decisions are not influenced by factors like a program recipient's gender, ethnicity, or wealth. (3) 
To make sure decisions are not influenced by officials’ biases. (4)
To reduce fraud against the government. (5) 
To make decisions which are more consistent and less “random”. (6) 
To reduce the number of bureaucrats/government officials. (7) 
To reduce the costs of government. (8) 


EPIS

Q31A. To reduce the time required to make decisions.
1-Acceptable 2-Unacceptable 3-Not sure 4-Refusal

Q31B. To make decisions which will be a better use of government money.
1-Acceptable 2-Unacceptable 3-Not sure 4-Refusal

Q31C. To make sure decisions are not influenced by factors like a program recipient's gender, ethnicity, or wealth.
1-Acceptable 2-Unacceptable 3-Not sure 4-Refusal

Q31D. To make sure decisions are not influenced by officials’ biases.
1-Acceptable 2-Unacceptable 3-Not sure 4-Refusal

Q31E. To reduce fraud against the government.
1-Acceptable 2-Unacceptable 3-Not sure 4-Refusal

Q31F. To make decisions which are more consistent and less “random”.
1-Acceptable 2-Unacceptable 3-Not sure 4-Refusal

Q31G. To reduce the number of bureaucrats/government officials.
1-Acceptable 2-Unacceptable 3-Not sure 4-Refusal

Q31H. To reduce the costs of government.
1-Acceptable 2-Unacceptable 3-Not sure 4-Refusal

*/

lab var q31a "To reduce the time required to make decisions."
lab var q31b "To make decisions which will be a better use of government money."
lab var q31c "To make sure decisions are not influenced by factors like a program recipient's gender, ethnicity, or wealth."
lab var q31d "To make sure decisions are not influenced by officials’ biases."
lab var q31e "To reduce fraud against the government."
lab var q31f "To make decisions which are more consistent and less “random”."
lab var q31g "To reduce the number of bureaucrats/government officials."
lab var q31h "To reduce the costs of government."

codebook q31a q31b q31c q31d q31e q31f q31g q31h


* Ordinal version
recode q31a q31b q31c q31d q31e q31f q31g q31h ///
	(1   = 1 "Acceptable") ///
	(2   = -1 "Unacceptable") ///
	(3 4 = 0 "Not sure") ///
	, gen(	alg_govt_acceptable_1_ord ///
			alg_govt_acceptable_2_ord ///
			alg_govt_acceptable_3_ord ///
			alg_govt_acceptable_4_ord ///
			alg_govt_acceptable_5_ord ///
			alg_govt_acceptable_6_ord ///
			alg_govt_acceptable_7_ord ///
			alg_govt_acceptable_8_ord ///
			) ///
	lab(alg_govt_acceptable_ord)

codebook alg_govt_acceptable_1_ord ///
			alg_govt_acceptable_2_ord ///
			alg_govt_acceptable_3_ord ///
			alg_govt_acceptable_4_ord ///
			alg_govt_acceptable_5_ord ///
			alg_govt_acceptable_6_ord ///
			alg_govt_acceptable_7_ord ///
			alg_govt_acceptable_8_ord

* Binary version
recode q31a q31b q31c q31d q31e q31f q31g q31h ///
	(1     = 1 "Acceptable") ///
	(2 3 4 = 0 "Not acceptable") ///
	, gen(	alg_govt_acceptable_1_bi ///
			alg_govt_acceptable_2_bi ///
			alg_govt_acceptable_3_bi ///
			alg_govt_acceptable_4_bi ///
			alg_govt_acceptable_5_bi ///
			alg_govt_acceptable_6_bi ///
			alg_govt_acceptable_7_bi ///
			alg_govt_acceptable_8_bi ///
			) ///
	lab(alg_govt_acceptable_bi)

codebook alg_govt_acceptable_1_bi ///
			alg_govt_acceptable_2_bi ///
			alg_govt_acceptable_3_bi ///
			alg_govt_acceptable_4_bi ///
			alg_govt_acceptable_5_bi ///
			alg_govt_acceptable_6_bi ///
			alg_govt_acceptable_7_bi ///
			alg_govt_acceptable_8_bi

*------------------------------------------------------------------------------*
* Left-Right scale
*------------------------------------------------------------------------------*

/*

EPIS: Q1. In politics people sometimes talk of “left” and “right”. Where would you place yourself on a
0 to 10 scale where 0 means the left and 10 means the right?
1-Left 2 3 4 5 6 7 8 9 10 11-Right 12-Don’t know 13-Refusal

This needs to be shifted down by 1, and have the missinv values set to the midpoint (5)

*/

codebook q1
tab q1

recode q1 ///
	(1  = 0 ) ///
	(2  = 1) ///
	(3  = 2) ///
	(4  = 3) ///
	(5  = 4) ///
	(6 12 13  = 5) ///
	(7  = 6) ///
	(8  = 7) ///
	(9  = 8) ///
	(10 = 9) ///
	(11 = 10) ///
	, gen(lr_scale) ///
	lab(lr_scale)

lab var lr_scale "Left-Right self placement (0-10)"

sum lr_scale

hist lr_scale, width(1)

*------------------------------------------------------------------------------*
* Populism
*------------------------------------------------------------------------------*

/* 

Canada: 

Do you agree or disagree with the following statements?

Strongly disagree (1)	Somewhat disagree (2)	Neither agree nor disagree (3)	Somewhat agree (4)	Strongly agree (5)


	The Canadian economy is rigged to advantage the rich and powerful (1) 
	Traditional parties and politicians don’t care about people like me (2) 
	Experts in this country don’t understand the lives of people like me (3) 
	To fix Canada, we need a strong leader willing to break the rules (4) 
	Canada needs a strong leader to take the country back from the rich and powerful (5) 
	Politicians should be able to say what’s on their minds regardless of what anyone else thinks about their views (6) 
	I trust the government to do the right thing (7) 


EPIS:
	Could you tell us how much you agree/disagree with the following statements:

	Q24A. To fix [country], we need a strong leader willing to break the rules

	Q24B. Traditional parties and politicians don’t care about people like me

	Q24C. I trust the government to do the right thing

	Q24D. [country] would be stronger if we stopped immigration

	Q24E. When jobs are scarce, employers should prioritize hiring people of this country over immigrants

	1-Strongly
	agree
	2-Somewhat
	agree
	3-Neither
	agree nor
	disagree
	4-Somewhat
	disagree
	5-Strongly
	disagree
	6-Don’t
	know
	7-
	Refusal

Okay, so the EPIS data only has 3 of the 7 populism items: Q24A, Q24B. and Q24C

Also, it has DK and Refuse options that we didn't have. I think I'll code those as neutral.

*/

lab var q24a "To fix [country], we need a strong leader willing to break the rules"
lab var q24b "Traditional parties and politicians don’t care about people like me"
lab var q24c "I trust the government to do the right thing"

recode q24a q24b q24c ///
	(1      = 5 "Strongly agree") ///
	(2      = 4 "Somewhat agree") ///
	(3 6 7  = 3 "Neither agree nor disagree") ///
	(4      = 2 "Somewhat disagree") ///
	(5      = 1 "Strongly disagree") ///
	, gen(populism_4 populism_2 populism_7) ///
	lab(populism)

* The last item is a reversal one, so when it's baked into the scale it's reversed. But that's dealt with later

********************************************************************************
* Merge in country-level data
********************************************************************************

merge m:1 country_recoded using "data/Support for Algorithmic Governance country level data rescaled.dta", keep(master match) nogen

********************************************************************************
* Saving results
********************************************************************************

save "data/EPIS_data recoded and labeled.dta", replace

